﻿@page "/course/{_courseId:guid}"
@layout ManagerLayout

@inject ICourseService CourseService
@inject NavigationManager NavigationManager
@inject IAssignmentService AssignmentService
@inject IMessageService MessageService
@using AntDesign.TableModels

<PageTitle>课程信息</PageTitle>
@*
	加一个作业列表根据用户身份不同而不同
	右上角老师添加布置作业按钮
	跳转作业布置和提交界面
*@
<Collapse DefaultActiveKey="@(new[]{"0"})">
	<Panel Header="课程信息" Key="0">
		@if(_course!=null)
		{
			<Descriptions Bordered Column="@column" Size="@DescriptionsSize.Small">
				<DescriptionsItem Title="名称">@_course.Name</DescriptionsItem>
				<DescriptionsItem Title="编号">@_course.CourseNumber</DescriptionsItem>				
				<DescriptionsItem Title="院系">@_course.DepartmentType.SchoolType.Name</DescriptionsItem>
				<DescriptionsItem Title="专业">@_course.DepartmentType.Name</DescriptionsItem>
				<DescriptionsItem Title="描述">@_course.Description</DescriptionsItem>
			</Descriptions>
		}
		else
		{
			<Empty/>
		}

	</Panel>
</Collapse>


<Divider></Divider>
<AuthorizeView Context="authorContext" Roles="教师">
	<Authorized>
		<Button @onclick="()=>ToPublishPage(_courseId)">发布作业</Button>
		<Table TItem="TeacherAssignmentState" DataSource="@_teacherAssignmentStates" OnRowClick="OnTeacherRowClick">
			<PropertyColumn Property="a=>a.Title" Title="名称">
				<p>@context.Title</p>
			</PropertyColumn>
			<PropertyColumn Property="a=>a.UpdateTime" Title="发布时间">
				<p>@context.UpdateTime.ToString("yy年MM月dd日")</p>				
			</PropertyColumn>
			<PropertyColumn Sortable="true" Property="a=>a.Deadline" Title="截止时间">
				<Tooltip Title="该日期的24:00截止">
					<p>@context.Deadline.ToString("yy年MM月dd日")</p>
				</Tooltip>
			</PropertyColumn>
			<PropertyColumn Property="a=>a.SubmissionCount" Title="状态">
				@if (context.Deadline < DateTime.Now)
				{
					<Tag Color="@PresetColor.Blue.ToString()">已截止</Tag>
				}

				@if (context.SubmissionCount == _studentCount)
				{
					<Tag Color="@PresetColor.Green.ToString()">已收齐</Tag>
				}
				else
				{
					<Tag Color="@PresetColor.Yellow.ToString()">未收齐</Tag>
				}
			</PropertyColumn>
			<PropertyColumn Property="a=>a.SubmissionCount" Title="提交人数">
				<p>@context.SubmissionCount / @_studentCount</p>
			</PropertyColumn>
			<ActionColumn Title="操作">
				<Button @onclick="()=>OpenDeleteConfirmModal(context.AssignmentId)">
					<p>删除</p>
				</Button>
				<Button @onclick="()=>ToEditPage(context.CourseId,context.AssignmentId)">
					<p>修改</p>
				</Button>
				<Button @onclick="()=>ToGardePage(context.CourseId,context.AssignmentId)">
					<p>批改</p>
				</Button>
			</ActionColumn>
				
		</Table>
		<Modal Title="确定删除"
			   Visible="@_modalVisible"
			   OnOk="@HandleModalOk"
			   OnCancel="()=>_modalVisible=false">
			@if (_selectAssignment != null)
			{
				<Alert Type="@AlertType.Warning"
					Message="@($"你真的要删除 {_selectAssignment.Title} 吗?")"
					Description="作业将会永久失去，相关信息将会被清空"
					ShowIcon="true"
					Closable />
			}
		</Modal>
	</Authorized>		
</AuthorizeView>


<AuthorizeView Context="authorContext" Roles="学生">
	<Authorized>
		<Table TItem="StudentAssignmentState" DataSource="@_studentAssignmentStates" OnRowClick="OnStudentRowClick">
			<PropertyColumn Property="a=>a.Title" Title="名称">
				<p>@context.Title</p>
			</PropertyColumn>
			<PropertyColumn Property="a=>a.UpdateTime" Title="发布时间">
				<p>@context.UpdateTime.ToString("yy年MM月dd日")</p>
			</PropertyColumn>
			<PropertyColumn Sortable="true" Property="a=>a.Deadline" Title="截止时间">
				<Tooltip Title="该日期的24:00截止">
					<p>@context.Deadline.ToString("yy年MM月dd日")</p>
				</Tooltip>
			</PropertyColumn>
			<PropertyColumn Property="a=>a.SubmissionCount" Title="状态">
				@if (context.Deadline < DateTime.Now)
				{
					<Tag Color="@PresetColor.Blue.ToString()">已截止</Tag>
				}
				@if (context.SubmissionCount <= 0)
				{
					<Tag Color="@PresetColor.Red.ToString()">未提交</Tag>
				}
				else
				{
					<Tag Color="@PresetColor.Green.ToString()">已提交</Tag>
				}
			</PropertyColumn>
			<PropertyColumn Property="a=>a.SubmissionCount" Title="提交次数">
				<p>@context.SubmissionCount</p>
			</PropertyColumn>
			<ActionColumn Title="操作">
				<Button @onclick="()=>ToSubmissionPage(context.CourseId,context.AssignmentId)">
					@if (context.SubmissionCount > 0)
					{
						<p>修改作业</p>
					}
					else
					{
						<p>提交作业</p>
					}

				</Button>
			</ActionColumn>
		</Table>
	</Authorized>
</AuthorizeView>


@code {
	[Parameter]
	public Guid _courseId{ get; set; }
	private Guid _userId { get; set; }

	[CascadingParameter]
	private Task<AuthenticationState>? _authenticationState { get; set; }

	private Models.Course _course { get; set; }

	// 或许可以考虑把这两个合并起来
	private List<StudentAssignmentState> _studentAssignmentStates { get; set; } = new();
	private List<TeacherAssignmentState> _teacherAssignmentStates { get; set; } = new();
	private int _studentCount = 0;

	protected override async Task OnParametersSetAsync() 
	{
		var courseResponse = await CourseService.GetByIdAsync(_courseId);
		if (courseResponse.Success && courseResponse.Data != null)
		{
			_course = courseResponse.Data;
		}

		await UpdateAssignmentsAsync();

		await base.OnParametersSetAsync();
	}

	private async Task UpdateAssignmentsAsync()
	{
		var state = await _authenticationState;
		var claim = state.User.FindFirst(ClaimTypes.NameIdentifier);
		var role = state.User.FindFirst(ClaimTypes.Role);
		if (claim != null && role != null && Guid.TryParse(claim.Value, out Guid temp))
		{
			_userId = temp;

			if (role.Value == "学生")
			{
				var assignmentStateResponse = await AssignmentService.GetStateByStudentCourseAsync(_userId, _courseId);
				if (assignmentStateResponse.Success && assignmentStateResponse.Data != null)
				{
					_studentAssignmentStates = assignmentStateResponse.Data;
				}
			}
			else if (role.Value == "教师")
			{
				var assignmentStateResponse = await AssignmentService.GetStateByTeacherCourseAsync(_userId, _courseId);
				if (assignmentStateResponse.Success && assignmentStateResponse.Data != null)
				{
					_teacherAssignmentStates = assignmentStateResponse.Data;
				}
				_studentCount = await CourseService.GetUserCount(_courseId);
			}
		}
	}

	private	void OnStudentRowClick(RowData<StudentAssignmentState> row)
	{

	}

	private void OnTeacherRowClick(RowData<TeacherAssignmentState> row)
	{

	}

	private void ToPublishPage(Guid courseId)
	{
		NavigationManager.NavigateTo($"/course/{courseId}/publish");
	}

	private void ToEditPage(Guid courseId,Guid assignmentId)
	{
		NavigationManager.NavigateTo($"/course/{courseId}/edit/{assignmentId}");
	}

	private void ToSubmissionPage(Guid courseId,Guid assignmentId)
	{
		NavigationManager.NavigateTo($"/course/{courseId}/submission/{assignmentId}");
	}
		
	private void ToGardePage(Guid courseId,Guid assignmentId)
	{
		NavigationManager.NavigateTo($"/course/{courseId}/grade/{assignmentId}");
	}

	private Dictionary<string, int> column = new Dictionary<string, int> 
	{
			{ "xxl", 3},
			{ "xl", 3},
			{ "lg", 2},
			{ "md", 2},
			{ "sm", 1},
			{ "xs", 1}
	};

	#region -删除作业-
	private bool _modalVisible = false;
	private Assignment _selectAssignment;
	private async void OpenDeleteConfirmModal(Guid assignmentId)
	{
		var response= await AssignmentService.GetByIdAsync(assignmentId);
		if (response.Success && response.Data!=null)
		{
			_selectAssignment = response.Data;
			_modalVisible = true;
		}
		else
		{
			await MessageService.Error($"获取该作业失败:{response.Message}");
		}

	}
	private async void HandleModalOk()
	{
		if (_selectAssignment != null)
		{
			var response = await AssignmentService.DeleteAssignment(_selectAssignment.AssignmentId);
			if (response.Success)
			{			
				await UpdateAssignmentsAsync();
				MessageService.Success("已完成删除操作");
			}
			else
			{
				MessageService.Error($"删除失败:{response.Message}");
			}
		}
		
		_modalVisible = false;
		StateHasChanged();
	}
	#endregion
}
